$(function () {
    $("#jqGrid").jqGrid({
        url: '../sparepart/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true},
            { label: '备件类型', name: 'spareTypeId', index: 'spare_type_id', width: 80 ,hidden:true},
            {label: '备件名称', name: 'spareName', index: 'spare_name', width: 80},
            {label: '单价', name: 'unitPrice', index: 'unit_price', width: 80 },
            {label: '币种', name: 'currency', index: 'currency', width: 80},
            {label: '语言编码', name: 'language', index: 'language', width: 80},
            {label: '数据状态（0启用、1停用）', name: 'status', index: 'status', width: 80,hidden:true},
            {label: '添加时间', name: 'addTime', index: 'add_time', width: 80,hidden:true},
            {label: '添加人', name: 'addUser', index: 'add_user', width: 80,hidden:true}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});
// Vue.use(VueQuillEditor)
var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        sparePart: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        querySpare:function(){
            var url = "../spareparttype/spareTypeList";
            $.ajax({
                type: "POST",
                url: url,
                data: JSON.stringify(new Object()),
                success: function(r){
                    console.log(r);
                    if(r.code === 0){
                        $("#spareSelect").empty();
                        for(var i=0;i<r.spareTypeList.length;i++)
                        {
                            $("#spareSelect").append("<option value='"+r.spareTypeList[i].id+"'>"+r.spareTypeList[i].typeName+"</option>");
                        }
                    }else{

                    }
                }
            });
        },
        add: function () {
            vm.querySpare();
            vm.showList = false;
            vm.title = "新增";
            vm.sparePart = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        onEditorBlur:function(quill) {
            console.log('editor blur!', quill)
        },
        onEditorFocus:function(quill) {
            console.log('editor focus!', quill)
        },
        onEditorReady:function(quill) {
            console.log('editor ready!', quill)
        },
        saveOrUpdate: function (event) {
            var url = vm.sparePart.id == null ? "../sparepart/save" : "../sparepart/update";
            //赋值
            var spareSelect = $("#spareSelect").val();
            var language = $("#languageSelect").val();
            var currency = $("#currencySelect").val();
            vm.sparePart.spareTypeId = spareSelect;
            vm.sparePart.currency = currency;
            vm.sparePart.language = language;
            console.log(JSON.stringify(vm.sparePart));
            $.ajax({
                type: "POST",
                url: url,
                data: JSON.stringify(vm.sparePart),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../sparepart/delete",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            vm.querySpare();
            $.get("../sparepart/info/" + id, function (r) {
                vm.sparePart = r.sparePart;
                $("#languageSelect").find("option[text='"+vm.sparePart.language+"']").attr("selected",true);
                $("#currencySelect").find("option[text='"+vm.sparePart.currency+"']").attr("selected",true);
                $("#spareSelect").find("option[text='"+vm.sparePart.spareTypeId+"']").attr("selected",true);
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        }
    }
});